package com.wujilin.okra.loader;

import android.content.Context;
import android.database.Cursor;

import com.wujilin.okra.database.Sql;

/**
 * The cursor loader for the basic SELECT statements.
 * 
 * @author Belin Wu
 *
 */
public class SelectCursorLoader extends CursorLoader {
	private boolean distinct;
	private String table;
	private String[] columns;
	private String selection;
	private String[] selectionArgs;
	private String groupBy;
	private String having;
	private String orderBy ;
	private String limit;
	
	private SelectCursorLoader(Builder builder) {
		super(builder.context);
		
		this.distinct = builder.distinct;
		this.table = builder.table;
		this.columns = builder.columns;
		this.selection = builder.selection;
		this.selectionArgs = builder.selectionArgs;
		this.groupBy = builder.groupBy;
		this.having = builder.having;
		this.orderBy = builder.orderBy;
		this.limit = builder.limit;
	}

	@Override
	public Cursor loadCursor() {
		return Sql.select(distinct, table, columns, selection, selectionArgs, groupBy, having, orderBy, limit);
	}
	
	public static class Builder {
		private Context context;
		
		private boolean distinct;
		private String table;
		private String[] columns;
		private String selection;
		private String[] selectionArgs;
		private String groupBy;
		private String having;
		private String orderBy ;
		private String limit;
		
		public Builder(Context context, String table) {
			this.context = context;
			this.table = table;
		}
		
		public Builder distinct(boolean distinct) {
			this.distinct = distinct;
			return this;
		}
		
		public Builder columns(String... columns) {
			this.columns = columns;
			return this;
		}

		public Builder selection(String selection) {
			this.selection = selection;
			return this;
		}
		
		public Builder selectionArgs(String... selectionArgs) {
			this.selectionArgs = selectionArgs;
			return this;
		}
		
		public Builder groupBy(String groupBy) {
			this.groupBy = groupBy;
			return this;
		}
		
		public Builder having(String having) {
			this.having = having;
			return this;
		}
		
		public Builder orderBy(String orderBy) {
			this.orderBy = orderBy;
			return this;
		}
		
		public Builder limit(String limit) {
			this.limit = limit;
			return this;
		}
		
		public SelectCursorLoader build() {
			return new SelectCursorLoader(this);
		}
	}
}
